package com.zhaodui.sys.service.impl;
import com.zhaodui.sys.service.FxjFileMainServiceI;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import com.zhaodui.sys.entity.FxjFileMainEntity;
import com.zhaodui.sys.page.FxjFileMainPage;
import com.zhaodui.sys.entity.FxjFileLogEntity;
import com.zhaodui.sys.entity.FxjFileListEntity;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import java.util.ArrayList;
import java.util.UUID;
import java.io.Serializable;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.core.util.MyClassLoader;
import org.jeecgframework.web.cgform.enhance.CgformEnhanceJavaInter;
import java.util.Map;
import java.util.HashMap;
import org.jeecgframework.minidao.util.FreemarkerParseFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.jeecgframework.core.util.ResourceUtil;

@Service("fxjFileMainService")
@Transactional
public class FxjFileMainServiceImpl extends CommonServiceImpl implements FxjFileMainServiceI {

	@Autowired
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
 	public void delete(FxjFileMainEntity entity) throws Exception{
 		super.delete(entity);
 	}
	
	public void addMain(FxjFileMainPage fxjFileMainPage) throws Exception {
		FxjFileMainEntity fxjFileMain = new FxjFileMainEntity();
		MyBeanUtils.copyBeanNotNull2Bean(fxjFileMainPage, fxjFileMain);
		//保存主信息
		this.save(fxjFileMain);
		/**保存-操作日志*/
		List<FxjFileLogEntity> fxjFileLogList = fxjFileMainPage.getFxjFileLogList();
		for(FxjFileLogEntity fxjFileLog:fxjFileLogList){
			//外键设置
			fxjFileLog.setFileNo(fxjFileMain.getFileNo());
			this.save(fxjFileLog);
		}
		/**保存-文件列表*/
		List<FxjFileListEntity> fxjFileListList = fxjFileMainPage.getFxjFileListList();
		for(FxjFileListEntity fxjFileList:fxjFileListList){
			//外键设置
			fxjFileList.setFileNo(fxjFileMain.getFileNo());
			this.save(fxjFileList);
		}
	}

	
	public void updateMain(FxjFileMainPage fxjFileMainPage) throws Exception{
		FxjFileMainEntity fxjFileMain = new FxjFileMainEntity();
		//保存主表信息
		if(StringUtil.isNotEmpty(fxjFileMainPage.getId())){
			fxjFileMain = findUniqueByProperty(FxjFileMainEntity.class, "id", fxjFileMainPage.getId());
			MyBeanUtils.copyBeanNotNull2Bean(fxjFileMainPage, fxjFileMain);
			this.saveOrUpdate(fxjFileMain);
		}else{
			this.saveOrUpdate(fxjFileMain);
		}
		//===================================================================================
		//获取参数
		Object fileNo0 = fxjFileMain.getFileNo();
		Object fileNo1 = fxjFileMain.getFileNo();
		//===================================================================================
		//1.操作日志数据库的数据
	    String hql0 = "from FxjFileLogEntity where 1 = 1 AND fileNo = ? ";
	    List<FxjFileLogEntity> fxjFileLogOldList = this.findHql(hql0,fileNo0);
		//2.操作日志新的数据
		List<FxjFileLogEntity> fxjFileLogList = fxjFileMainPage.getFxjFileLogList();
	    //3.筛选更新明细数据-操作日志
		if(fxjFileLogList!=null &&fxjFileLogList.size()>0){
			for(FxjFileLogEntity oldE:fxjFileLogOldList){
				boolean isUpdate = false;
				for(FxjFileLogEntity sendE:fxjFileLogList){
					//需要更新的明细数据-操作日志
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-操作日志
		    		super.delete(oldE);
	    		}
	    		
			}
			//4.持久化新增的数据-操作日志
			for(FxjFileLogEntity fxjFileLog:fxjFileLogList){
				if(oConvertUtils.isEmpty(fxjFileLog.getId())){
					//外键设置
					fxjFileLog.setFileNo(fxjFileMain.getFileNo());
					this.save(fxjFileLog);
				}
			}
		}
		//===================================================================================
		//1.文件列表数据库的数据
	    String hql1 = "from FxjFileListEntity where 1 = 1 AND fileNo = ? ";
	    List<FxjFileListEntity> fxjFileListOldList = this.findHql(hql1,fileNo1);
		//2.文件列表新的数据
		List<FxjFileListEntity> fxjFileListList = fxjFileMainPage.getFxjFileListList();
	    //3.筛选更新明细数据-文件列表
		if(fxjFileListList!=null &&fxjFileListList.size()>0){
			for(FxjFileListEntity oldE:fxjFileListOldList){
				boolean isUpdate = false;
				for(FxjFileListEntity sendE:fxjFileListList){
					//需要更新的明细数据-文件列表
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-文件列表
		    		super.delete(oldE);
	    		}
	    		
			}
			//4.持久化新增的数据-文件列表
			for(FxjFileListEntity fxjFileList:fxjFileListList){
				if(oConvertUtils.isEmpty(fxjFileList.getId())){
					//外键设置
					fxjFileList.setFileNo(fxjFileMain.getFileNo());
					this.save(fxjFileList);
				}
			}
		}
	}

	
	public void delMain(FxjFileMainEntity fxjFileMain) throws Exception{
		//删除主表信息
		this.delete(fxjFileMain);
		//===================================================================================
		//获取参数
		Object fileNo0 = fxjFileMain.getFileNo();
		Object fileNo1 = fxjFileMain.getFileNo();
		//===================================================================================
		//删除-操作日志
	    String hql0 = "from FxjFileLogEntity where 1 = 1 AND fileNo = ? ";
	    List<FxjFileLogEntity> fxjFileLogOldList = this.findHql(hql0,fileNo0);
		this.deleteAllEntitie(fxjFileLogOldList);
		//===================================================================================
		//删除-文件列表
	    String hql1 = "from FxjFileListEntity where 1 = 1 AND fileNo = ? ";
	    List<FxjFileListEntity> fxjFileListOldList = this.findHql(hql1,fileNo1);
		this.deleteAllEntitie(fxjFileListOldList);
	}
	
 	
}